package com.liming.stock.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liming.common.exception.BusinessException;
import com.liming.common.pojo.dto.stock.warehouse.WarehouseAddDTO;
import com.liming.common.pojo.dto.stock.warehouse.WarehousePageQuery;
import com.liming.common.pojo.dto.stock.warehouse.WarehouseUpdateDTO;
import com.liming.common.pojo.entity.stock.Warehouse;
import com.liming.common.pojo.vo.PageVO;
import com.liming.common.pojo.vo.stock.warehouse.WarehouseVO;
import com.liming.common.util.BeanUtils;
import com.liming.common.util.PageUtils;
import com.liming.common.util.mybatisplus.MPUtil;
import com.liming.stock.mapper.WarehouseMircoMapper;
import com.liming.stock.service.WarehouseMircoService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.function.Function;

/**
 * @author liming
 * @date 2025/1/23
 */
@Service
@AllArgsConstructor
public class WarehouseMircoServiceImpl extends ServiceImpl<WarehouseMircoMapper, Warehouse>
        implements WarehouseMircoService, MPUtil<Warehouse> {

    @Override
    public PageVO<WarehouseVO> pageByQuery(WarehousePageQuery query) {
        IPage<WarehouseVO> iPage = lambdaQuery()
                .like(StrUtil.isNotBlank(query.getName()), Warehouse::getName, query.getName())
                .eq(ObjectUtil.isNotNull(query.getStatus()), Warehouse::getStatus, query.getStatus())
                .eq(ObjectUtil.isNotNull(query.getType()), Warehouse::getType, query.getType())
                .eq(ObjectUtil.isNotNull(query.getProvince()), Warehouse::getProvince, query.getProvince())
                .eq(ObjectUtil.isNotNull(query.getCity()), Warehouse::getCity, query.getCity())
                .eq(ObjectUtil.isNotNull(query.getDistrict()), Warehouse::getDistrict, query.getDistrict())
                .eq(ObjectUtil.isNotNull(query.getStreet()), Warehouse::getStreet, query.getStreet())
                .page(setPage(query))
                .convert(warehouse -> BeanUtils.toBean(warehouse, WarehouseVO.class));
        return PageUtils.convert(iPage, Function.identity());
    }

    @Override
    public Boolean add(WarehouseAddDTO warehouseAddDTO) {
        checkSimilarCodeOrName(null, warehouseAddDTO.getCode(), warehouseAddDTO.getName());
        Warehouse save = new Warehouse();
        org.springframework.beans.BeanUtils.copyProperties(warehouseAddDTO, save);
        Assert.isTrue(save(save), () -> new BusinessException("仓库保存失败"));
        return true;
    }

    @Override
    public Boolean editById(WarehouseUpdateDTO warehouseUpdDTO) {
        checkSimilarCodeOrName(warehouseUpdDTO.getId(), warehouseUpdDTO.getCode(), warehouseUpdDTO.getName());
        Warehouse update = new Warehouse();
        org.springframework.beans.BeanUtils.copyProperties(warehouseUpdDTO, update);
        Assert.isTrue(updateById(update), () -> new BusinessException("仓库修改失败"));
        return true;
    }

    private void checkSimilarCodeOrName(Long id, String code, String name) {
        lambdaQuery()
                .and(ObjectUtil.isNotNull(id), lambda -> lambda.ne(Warehouse::getId, id))
                .and(lambda -> {
                    lambda.eq(Warehouse::getCode, code).or().eq(Warehouse::getName, name);
                })
                .oneOpt()
                .ifPresent(warehouse -> {
                    throw new BusinessException("仓库编码或名称已存在");
                });
    }
}
